#include <bits/stdc++.h>

using namespace std;

class Solution
{
public:
    vector<int> powerfulIntegers(int x, int y, int bound)
    {
        int big = x > y ? x : y;
        int small = x + y - big;
        vector<int> ret;
        set<int> s;
        int num = 2, ab = 0, as = 0;
        if (x == 1 || y == 1)
        {
            if (big == 1)
            {
                if (2 <= bound)
                {
                    ret.push_back(2);
                }
            }
            else
            {
                int cnt = 1;
                while (num <= bound)
                {
                    ret.push_back(num);
                    num = pow(big, cnt) + 1;
                    ++cnt;
                }
            }
            return ret;
        }
        while (num <= bound)
        {
            while (num <= bound)
            {
                s.insert(num);
                ++as;
                num = pow(big, ab) + pow(small, as);
            }
            as = 0;
            ++ab;
            num = pow(big, ab) + pow(small, as);
        }
        for (auto e : s)
        {
            ret.push_back(e);
        }
        return ret;
    }
};